//
//  DiaryDataBase.m
//  Cooking
//
//  Created by ZhiPeng.Y on 15/9/9.
//  Copyright (c) 2015年 ZP.Y. All rights reserved.
//

// 用于执行数据库语句

#import "DiaryDataBase.h"
#import "DB.h"
#import "DBModel.h"
#import "RecommendModel.h"
#import "FoodDetailsModel.h"

@implementation DiaryDataBase

#pragma mark - 
#pragma mark 推荐页
//插入
+ (void)insertWithRecommend:(RecommendModel *)recommend {
    sqlite3 * db = [DB dbOpen];
    
    NSString *str = [NSString stringWithFormat:
                     @"insert into Collect(vegetable_id, name, imagePathThumbnails) values ('%@', '%@', '%@')",
                     recommend.vegetable_id, recommend.name, recommend.imagePathThumbnails];
    
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    
    // 判断是否执行成功
    if(result == SQLITE_OK){
        NSLog(@"插入成功 %d",result);
    }else{
        NSLog(@"插入失败 %d",result);
    }
}

//删除
+ (void)deleteWithRecommendId:(NSString *)vegetable_id {
    sqlite3 *db = [DB dbOpen];
    NSString * str = [NSString stringWithFormat:@"delete from Collect where vegetable_id = '%@' ",vegetable_id];
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    if(result == SQLITE_OK){
        NSLog(@"删除成功 %d",result);
    }else{
        NSLog(@"删除失败 %d",result);
    }
}

//查询(暂时不使用)
+ (RecommendModel *)selectWithRecommendId:(NSString *)vegetable_id {
    sqlite3 *db = [DB dbOpen];
    sqlite3_stmt * stmt = nil;
    
    NSString * str = [NSString stringWithFormat:@"select vegetable_id, name, imagePathThumbnails from Collect where vegetable_id = '%@'", vegetable_id];
    // 执行
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *vegetable_id = sqlite3_column_text(stmt, 0); // 参数分别为替身和列
            const unsigned char *name = sqlite3_column_text(stmt, 1);
            const unsigned char *imagePathThumbnails = sqlite3_column_text(stmt, 2);
            
            NSString *idStr = [NSString stringWithUTF8String:(const char *)vegetable_id];
            NSString *nameStr = [NSString stringWithUTF8String:(const char *)name];
            NSString *imagePathStr = [NSString stringWithUTF8String:(const char *)imagePathThumbnails];
            RecommendModel *recommend = [[RecommendModel alloc] init];
            recommend.vegetable_id = idStr;
            recommend.name = nameStr;
            recommend.imagePathThumbnails = imagePathStr;
            
            sqlite3_finalize(stmt);
            
            return recommend;
        }
    }
    // 释放替身
    sqlite3_finalize(stmt);
    
    return nil;
}

//查询
+ (NSArray *)selectRecommendAll {
    sqlite3 *db = [DB dbOpen];
    sqlite3_stmt * stmt = nil;
    
    NSString * str = [NSString stringWithFormat:@"select vegetable_id, name, imagePathThumbnails from Collect"];
    // 执行
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    
    NSMutableArray * array = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *vegetable_id = sqlite3_column_text(stmt, 0); // 参数分别为替身和列
            const unsigned char *name = sqlite3_column_text(stmt, 1);
            const unsigned char *imagePathThumbnails = sqlite3_column_text(stmt, 2);
            
            NSString *idStr = [NSString stringWithUTF8String:(const char *)vegetable_id];
            NSString *nameStr = [NSString stringWithUTF8String:(const char *)name];
            NSString *imagePathStr = [NSString stringWithUTF8String:(const char *)imagePathThumbnails];
            RecommendModel *recommend = [[RecommendModel alloc] init];
            recommend.vegetable_id = idStr;
            recommend.name = nameStr;
            recommend.imagePathThumbnails = imagePathStr;
            [array addObject:recommend];
        }
    }
    
    // 释放替身
    sqlite3_finalize(stmt);
    return array;
}














@end